package com.dongdong.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.dongdong.mapper.TbContentMapper;
import com.dongdong.model.TbContent;
import com.dongdong.model.TbContentExample;
import com.dongdong.service.ContentService;
import com.dongdong.vo.ContentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentMapper contentMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<ContentVO> getContentList() {
        List<TbContent> tbContents = contentMapper.selectByExample(null);
        List<ContentVO> collect = tbContents.stream().map(contentlist -> {
            ContentVO vo = new ContentVO();
            BeanUtils.copyProperties(contentlist, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public TbContent toUpdategetById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int InsertContent(ContentVO contentVO) {
        TbContent content = new TbContent();
        BeanUtils.copyProperties(contentVO, content);
        int i = contentMapper.insert(content);
        return i;
    }

    @Override
    public int deleteContent(Long[] arr) {
        TbContentExample example = new TbContentExample();
        if (arr.length == 0) {
            return 0;
        }
        example.createCriteria().andIdIn(Arrays.asList(arr));
        int i = contentMapper.deleteByExample(example);
        return i;
    }

    @Override
    public List<ContentVO> getContentByCateGoryId(Long categoryId) {
        TbContentExample example = new TbContentExample();

        if (categoryId == null) {
            return null;
        }
        TbContentExample.Criteria criteria = example.createCriteria().andCategoryIdEqualTo(categoryId);
        if (categoryId != null && !"".equals(categoryId)) {
            criteria.andCategoryIdEqualTo(categoryId);
        }
        String Key = "CateGoryId";//首页轮播图
        String ksy = stringRedisTemplate.opsForValue().get(Key);
        if (StrUtil.isEmpty(ksy)) {//redis等于空
            List<TbContent> tbContents = contentMapper.selectByExample(example);
            List<ContentVO> collect = tbContents.stream().map(contentlist -> {
                ContentVO vo = new ContentVO();
                BeanUtils.copyProperties(contentlist, vo);
                return vo;
            }).collect(Collectors.toList());

            //将数据存储到redis中
            stringRedisTemplate.opsForValue().set(Key, JSONObject.toJSONString(collect));//将集合转换为json字符串
            return collect;
        } else {
            //redis有数据 ， 直接获取redis的数据
            String redis = stringRedisTemplate.opsForValue().get(Key);
            System.out.println(redis);
            List<ContentVO> contentVOS = JSONObject.parseArray(redis, ContentVO.class);//json转对象

            //遍历集合
            Long categoryId1 = null;
            for (ContentVO contentVO : contentVOS) {
                categoryId1 = contentVO.getCategoryId();
            }
            //判断缓存中是否和二次传递的id一致
            if (!categoryId.equals(categoryId1)) {
                //不一致 查询数据库
                List<TbContent> tbContents = contentMapper.selectByExample(example);
                stringRedisTemplate.opsForValue().set(Key, JSONObject.toJSONString(tbContents));
                List<ContentVO> collect = tbContents.stream().map(contetnlist -> {
                    ContentVO vo = new ContentVO();
                    BeanUtils.copyProperties(contetnlist, vo);
                    return vo;
                }).collect(Collectors.toList());
                return collect;
            }
            return contentVOS;
        }
    }
}
